package com.hsh.erp.domain.common;

import com.hsh.erp.constants.ResultConstants;
import lombok.Data;

@Data
public class Result<T> {

    private boolean success;

    private String code;

    private String msg;

    private T data;

    /**
     * 根据数字确认成功或失败
     *
     * @param successNum <=0:失败,
     *                   >0:成功
     * @return
     */
    public static Result create(int successNum) {
        if (successNum <= 0) {
            return fail(String.format(ResultConstants.MSG_FAIL_NUM, successNum), successNum);
        } else {
            return success(String.format(ResultConstants.MSG_SUCCESS_NUM, successNum), successNum);
        }
    }

    /**
     * 根据数字确认成功或失败
     *
     * @param successNum <=0:失败,
     *                   >0:成功
     * @return
     */
    public static Result create(int successNum, int failNum) {
        // 操作失败
        if (successNum <= 0) {
            return fail(String.format(ResultConstants.MSG_FAIL_NUM, successNum), successNum);
        }
        // 操作成功
        return success(String.format(ResultConstants.MSG_SUCCESS_AND_FAIL_NUM, successNum, failNum), successNum);

    }

    /**
     * 根据数字确认成功或失败
     *
     * @param success
     * @return
     */
    public static Result create(boolean success) {
        if (success) {
            return success();
        } else {
            return fail();
        }
    }

    /**
     * 成功---默认返回
     *
     * @return
     */
    public static Result success() {
        Result result = new Result();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = ResultConstants.MSG_SUCCESS;
        return result;
    }

    /**
     * 成功---指定返回msg
     *
     * @param msg
     * @return
     */
    public static Result success(String msg) {
        Result result = new Result();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = msg;
        result.data = msg;
        return result;
    }

    /**
     * 成功---指定返回data
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = ResultConstants.MSG_SUCCESS;
        result.data = data;
        return result;
    }

    /**
     * 成功---指定返回data和msg
     *
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<>();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = msg;
        result.data = data;
        return result;
    }

    /**
     * 失败---默认返回
     *
     * @return
     */
    public static Result fail() {
        Result result = new Result();
        result.success = false;
        result.code = ResultConstants.CODE_FAIL;
        result.msg = ResultConstants.MSG_FAIL;
        return result;
    }

    /**
     * 失败---指定返回msg
     *
     * @return
     */
    public static Result fail(String msg) {
        Result result = new Result<>();
        result.success = false;
        result.code = ResultConstants.CODE_FAIL;
        result.msg = msg;
        result.data = msg;
        return result;
    }

    /**
     * 失败---指定返回data
     *
     * @return
     */
    public static <T> Result<T> fail(T data) {
        Result<T> result = new Result<>();
        result.success = false;
        result.code = ResultConstants.CODE_FAIL;
        result.msg = ResultConstants.MSG_FAIL;
        result.data = data;
        return result;
    }


    /**
     * 失败---指定返回data和msg
     *
     * @return
     */

    public static <T> Result<T> fail(String msg, T data) {
        Result result = new Result();
        result.success = false;
        result.code = ResultConstants.CODE_FAIL;
        result.msg = msg;
        result.data = data;
        return result;
    }

}
